package com.sumainfor.pc.gmio.service.impl;

import com.sumainfor.common.Constants;
import com.sumainfor.common.util.ContextUtils;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.utlis.ComUtils;
import com.sumainfor.common.utlis.ParamsCommon;
import com.sumainfor.pc.gmio.dao.*;
import com.sumainfor.pc.gmio.service.HoSurveyTenantService;
import com.sumainfor.pc.gmio.service.HoSurveyTopicService;
import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.*;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 问卷调查
 */
@Service
public class HoSurveyTenantSerivceImpl implements HoSurveyTenantService {

    private ReentrantLock lock = new ReentrantLock(false);
    Logger log = LoggerFactory.getLogger(HoSurveyTenantSerivceImpl.class);
    @Autowired
    private HoSurveyTenantMapper hoSurveyTenantMapper;
    @Autowired
    private HoSurveyAnswerMapper hoSurveyAnswerMapper;
    @Autowired
    private UtilsMapper utilsMapper;
    /**
     * 新增问卷
     * @param params
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public JsonResult addHoSurveyTenant(Map<String, Object> params) {
        JsonResult result=new JsonResult();
        /**
         * 加锁操作
         */
        lock.lock();
        try{
            try {
                if (params.get("TenId")==null || params.get("HouInfoId")==null ||params.get("Remarks")==null
                        ||params.get("surTenList")==null ||params.get("TenType")==null){
                    return result.putFailed("参数不正确");
                }
                //问卷编号
                String UUID="";
                Map<String,Object>uuid=utilsMapper.getUUID(new HashMap<String,Object>(){{
                    put("uuid", "TenSurId");
                    put("database", Constants.DATA_SOURCE_GMIO);
                    put("surface", "tcgmio_survey_tenant");
                }});
                if(uuid!=null &&(!uuid.get("uuid").toString().equals(""))){
                    int number=Integer.valueOf(uuid.get("uuid").toString());
                    UUID= ComUtils.getLocalTrmSeqNum("ts",number);
                }else{
                    UUID=ComUtils.getLocalTrmSeqNum("ts",0);
                }
                Map<String,Object> map=new HashMap<String,Object>();
                map.put("TenSurId",UUID);//问卷调查编号
                map.put("by", "auto");//操作人
                map.put("TenId",params.get("TenId"));// 租客编号
                map.put("HouInfoId",params.get("HouInfoId"));// 租客编号
                map.put("Remarks",params.get("Remarks"));// 信息备注
                map.put("TenType",params.get("TenType"));//问卷类型

                map.put("term", "1");// 创建端 Insert端： 1 租客端 2市场部 3 工程部   4运营部   5财务部  6总经办 7发展部
                map.put("fnc",
                        this.getClass().getSimpleName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName());// 创建Function名
                ParamsCommon.commonInsertParms(map);
                map.put("CreateTime",map.get("instm"));//问卷提交时间
                Integer count=hoSurveyTenantMapper.addHoSurveyTenant(map);
                //获取问卷调查的问题集合
                if (count>0){
                    List<Map<String,Object>> surveyList=(List<Map<String,Object>>)params.get("surTenList");
                    for (Map<String,Object> surveymap : surveyList){
                        surveymap.putAll(map);
                        System.out.println("surveymap.get(\"surveyList\"):"+surveymap.get("surveyList"));
                        List<Map<String,Object>> anList=(List<Map<String,Object>>)surveymap.get("surveyList");//答案数组
                        System.out.println("anList:"+anList);
                        if (anList!=null && anList.size()>0){
                            for (Map<String,Object> an: anList){
                                System.out.println("anmap:"+an);
                                surveymap.put("SurCon",an.get("SurCon"));
                                surveymap.put("Answer",an.get("SurContent"));
                            }
                        }
                        hoSurveyAnswerMapper.addHoSurveyAnswer(surveymap);
                    }
                    result.putSuccess("操作成功");
                }else{
                    result.putFailed("服务器繁忙");
                }

            } catch (Exception e) {
                result.putFailed("服务器繁忙");
                e.printStackTrace();
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            }
        }
        finally {
            lock.unlock();
        }
        return result;
    }
}
